home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-02-27 | 6.8 KB | 287 lines | [TEXT/CWIE] |
- /* SK8 © 1997 Apple Computer, Inc.
- This code is protected under the current SK8 License
- See http://sk8.research.apple.com/ for more information
- Apple Research Laboratories
- */
-
-
- //This file implements the collection protocol
- //as well as some helper functions
-
- public abstract class collection {
-
- public abstract visitstate initialvisitstate(); //returns null if empty
-
- public abstract boolean isfinalvisitstate(visitstate inState);
-
- public abstract visitstate succeedingvisitstate(visitstate inState);
-
- public abstract Object elementatvisitstate(visitstate inState);
-
- public abstract void setelementatvisitstate(visitstate inState, Object inElement) ;
-
- public abstract void removevisitstate(visitstate inState);
-
- // in the lisp version of the List Protocol,
- // insertatvisitstate returns the collection.
- // since the caller already has a handler to the collection
- // to make the call, it makes more sense for function
- // to return a more interesting value, like the visit
- // state of the new element
-
- // If and only if the collection is empty, inState may be null
- public visitstate insertatvisitstate(visitstate inState, Object inElement){
- return insertatvisitstate(inState, inElement, false);
- }
-
- public abstract visitstate insertatvisitstate(visitstate inState, Object inElement, boolean inInsertAfter);
-
- public abstract int indexatvisitstate(visitstate inState);
-
- public abstract visitstate visitstateatindex(int inIndex);
-
- //must override this one!
- protected void checkvisitstatetype(visitstate inState) {
- throw new IllegalArgumentException("Incompatable visitstate class passed");
- }
-
-
- //public abstract Key keyatvisitstate(int inColumn, visitstate inState);
- //public abstract visitstate visitstateatkey(Key inKey);
- //public abstract void setelementatkey(Key inKey, Object inElement);
-
- ///HELPERS!!!
- public Object first() {
- return this.elementatvisitstate(this.initialvisitstate());
- }
-
- public Object last() {
- return this.elementatvisitstate(this.finalvisitstate());
- }
-
-
- public visitstate finalvisitstate() {
- visitstate vs;
-
- vs = this.initialvisitstate();
- while ((vs != null) && (isfinalvisitstate(vs) != true))
- vs = succeedingvisitstate(vs);
-
- return vs;
- }
-
- public boolean empty () {
- return (initialvisitstate() == null);
- }
-
- public int length () {
- visitstate vs = initialvisitstate();
- int count = 0;
- while (vs != null) {
- count++;
- if (isfinalvisitstate(vs) == true) {
- vs = null; }
- else vs = succeedingvisitstate(vs);
- }
- return count;
- }
-
-
- public int position (Object obj) {
- visitstate vs = initialvisitstate();
- int count = 0;
- if ((obj == null) || (vs == null))
- return -1;
- else {
- int returnValue = -1;
- while ((vs != null)) {
- count++;
- if (obj.equals(elementatvisitstate(vs))) {
- returnValue = count;
- vs = null; }
- else vs = succeedingvisitstate(vs);
- }
- return returnValue;
- }
- }
-
- public boolean contains (Object obj) {
- return (! (this.position(obj) == -1));
- }
-
-
- //For stacks
- public void push (Object obj1) {
- visitstate vs = this.initialvisitstate();
- this.insertatvisitstate(vs, obj1, false);
- }
-
-
- public void pushend (Object obj1) {
- visitstate vs = this.finalvisitstate();
- this.insertatvisitstate(vs, obj1, true);
- }
-
-
-
- public Object pop () {
- visitstate vs = initialvisitstate();
- if (vs == null) {
- return null;
- } else {
- Object obj = this.elementatvisitstate(vs);
- this.removevisitstate(vs);
- return obj;
- }
- }
-
- //
- public visitstate findvisitstateforelement (Object element) {
- visitstate vs = initialvisitstate();
- visitstate resultVS = null;
- while ((resultVS == null) && (vs != null)) {
- if (element == elementatvisitstate(vs))
- resultVS = vs;
- if (isfinalvisitstate(vs) == true) {
- vs = null; }
- else vs = succeedingvisitstate(vs);
- }
- return resultVS;
- }
-
-
- public void removeElement (Object element) {
- visitstate vs = findvisitstateforelement(element);
- if (vs != null)
- removevisitstate(vs);
- }
-
-
-
- //nth and setnth
-
- public Object nth (int n) {
- visitstate vs = initialvisitstate();
- int curcount = 1;
- while ((curcount != n) && (vs != null)) {
- curcount++;
- if (isfinalvisitstate(vs) == true) {
- vs = null; }
- else vs = succeedingvisitstate(vs);
- }
- if (vs == null)
- return null;
- else
- return this.elementatvisitstate(vs);
- }
-
- //Need the following function because in Java
- //primitives like ints and floats are not "Objects"
- public int nthint (int n) {
- Integer res = (Integer)this.nth(n);
- return res.intValue();
- }
- public float nthfloat (int n) {
- Float res = (Float)this.nth(n);
- return res.floatValue();
- }
-
-
- //Convient functions for nth element manipulations
- public boolean setnth (int n, Object val) {
- visitstate vs = initialvisitstate();
- int count = 0;
- while ((count != n) && (vs != null)) {
-
- count++;
- if (isfinalvisitstate(vs) == true) {
- vs = null; }
- else vs = succeedingvisitstate(vs);
- }
- if (vs == null)
- return false;
- else {
- this.setelementatvisitstate(vs,val);
- return true;
- }
- }
-
- public visitstate nthvisitstate(int n) {
- if ((n < 1 ) || (n > length()))
- return null;
- else {
- int count = 1;
- visitstate vs = initialvisitstate();
- while ((count != n) && (vs != null)) {
- count++;
- vs = succeedingvisitstate(vs);
- }
- return vs;
- }
- }
-
- public void insertnth (int n, Object val) {
- if (n == 1)
- push(val);
- else if (n == length() + 1)
- pushend(val);
- else if (( n < 1 ) || (n > length() + 1))
- throw new IndexOutOfBoundsException("out of bounds!");
- else {
- visitstate vs = nthvisitstate(n);
- insertatvisitstate(vs, val, false);
- }
- }
-
- public list range (int start, int end) {
- list res = new list();
- int currentcount = 0;
- visitstate sscurrentstate = this.initialvisitstate();
- Object it = null;
- while (! (sscurrentstate == null)) {
- it = this.elementatvisitstate(sscurrentstate);
- currentcount = (currentcount + 1);
- if ((currentcount >= start)) {
- res.push(it);
- if ((currentcount >= end)) {
- break;
- }
- }
- sscurrentstate = this.succeedingvisitstate(sscurrentstate);
- }
- return res.reverse();
- }
-
-
- // A printing function so collections print out nicely.
- public void printcollection () {
- Object currentActor;
- visitstate vs = initialvisitstate();
- while (vs != null) {
- currentActor = elementatvisitstate(vs);
- System.out.println(currentActor);
- if (isfinalvisitstate(vs) == true) {
- vs = null; }
- else vs = succeedingvisitstate(vs);
- }
- }
-
- public String toString(){
- visitstate vs = this.initialvisitstate();
- Object e;
- String str = "list{";
-
- while (vs != null){
- try{
- e = this.elementatvisitstate(vs);
- str = str + e.toString() + " ";
- vs = this.succeedingvisitstate(vs);
- } catch (Exception exc) {
- str = str + "...}";
- break;
- }
- }
- return str + "}";
- }
-
- }